var util = require('../../utils/util.js');
var app = getApp();
var md5 = require("../../utils/md5.js");
var uid = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    util:util,
    tc: 0,
    addressArr:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (data) {
    var tc = app.get_tc();
    var that = this;
    that.setData({
      tc: tc,
    });
    if (util.orgid == 13078){
      var titlename = tc == 1 ? '選擇臺號' : '选择台号';
    }
    if(util.orgid != 13078){
      var titlename = tc == 1 ? '自提配送点' : '自提配送点';
    }
    wx.setNavigationBarTitle({
      title: titlename,
    }); 
    //自提点地址
    util.api({
      data: {
        publickey: util.publiccode,
        ac: "getpeisong",
        uid: util.orgid
      },
      success: function (responseData) {
        var data = responseData.data;
        console.log("配送地址：", data);
        if (data && data.length > 0) {
          that.setData({
            addressArr: data
          });
        }
      }
    });
  },
 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  selectAddr: function(e){
    var ps_addr = e.currentTarget.dataset.addr;

    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];  //当前页面
    var prevPage = pages[pages.length - 2]; //上一个页面

    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    prevPage.setPSAddress(ps_addr);

    wx.navigateBack({
      delta: 1
    })
  },

  goAtherPage: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.navigateTo({ url: url })
  },

  makeCallPhone: function (e) {
    var phoneNum = e.currentTarget.dataset.number;
    console.log("phoneNum:" + phoneNum);
    wx.makePhoneCall({
      phoneNumber: phoneNum,
      success: function (e) {
        console.log("make call success:", JSON.stringify(e));
      },
      fail: function (e) {
        console.log("make call fail:", JSON.stringify(e));
      }
    })
  },

  showEWM: function (e) {
    var shareImgUrl = e.currentTarget.dataset.imgsrc;
    wx.previewImage({
      current: shareImgUrl,
      urls: [shareImgUrl]
    })
  },

  goMapView: function(e){
    var addr = e.currentTarget.dataset.addr;
    var lat = parseFloat(addr.lat);
    var lon = parseFloat(addr.lon);
    wx.openLocation({
      latitude: lat,
      longitude: lon,
      scale: 20,
      name: addr.name,
      address: addr.address 
    })
  },
})